﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Acciones iniciales para verificar usuario loggeado y visibilidad de menus
        var master = Master as SiteMaster;
        if (master != null)
        {
            master.AccionesIniciales();
        }
        //verificar si el usuario es valido
        var rol = (Int32)Page.Session["rol"];
        if (rol != 2)
        {
            //todos pueden ver esta pagina
            //Response.Redirect("About.aspx");
        }
    }

    public void solicitud(object sender, EventArgs e)
    {
        //en el string idusuario se debe de poner el id del usuario que esta logeado.
        String idusuario = "1";
        MySqlConnection myConnection = new MySqlConnection();
        myConnection.ConnectionString = "Persist Security Info=False;Username=root;Password=;database=dad;server=localhost;Connect Timeout=30";
        myConnection.Open();
        var q = String.Format("SELECT solicitudes_id, motivo, estado FROM solicitudes WHERE enviada_por_id='{0}'", idusuario);
        

        //create command and assign the query and connection from the constructor
        MySqlCommand cmd = new MySqlCommand(q, myConnection);

        var cont = "<table><tbody>";
        cont += "<tr><td>Numero Solicitud</td><td>Solicitud</td><td>Estatus</td></tr>";

        MySqlDataReader dataReader = cmd.ExecuteReader();
        //Read the data and store them in the list
        while (dataReader.Read())
        {
            var numsolicitud = dataReader["solicitudes_id"];
            var solicitud = dataReader["motivo"];
            var estatus = dataReader["estado"];
            var x = String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",numsolicitud, solicitud,estatus);
            cont += x;

        }
        cont += "</table></tbody>";

        //close Data Reader
        dataReader.Close();
        //close Connection
        myConnection.Close();

        //"<table><tbody><td>{0}</td><td>{1}</td></tbody></table>"

        contenido.Text = cont;
    
    }

    public void creasolicitud(object sender, EventArgs e)
    { 
    
    }
    protected void aceptar_Click(object sender, EventArgs e)
    {

        Type cstype = this.GetType();
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        UsersManager usersManager = new UsersManager();
        System.Int32 id1 = (System.Int32)Page.Session["userId"];
        string id = id1.ToString();

        try
        {
            usersManager.cambiarContrasena(id, nueva.Text);

            if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
            {
                String cstext = "alert('Contraseña cambiada con exito');";
                cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
            }
        }
        catch (Exception ex)
        {
            if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
            {
                String cstext = "alert('Error al cambiar la contraseña. intente de nuevo');";
                cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
            }
        }
    }
}